//
//  Person.m
//  OC06_集合遍历_数组排序
//
//  Created by dllo on 16/5/27.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "Person.h"

@implementation Person
-(instancetype)initWithName:(NSString *)name withAge:(NSInteger)age withSex:(NSString *)sex{
    self = [super init];
    if(self){
        _name = name;
        _age = age;
        _sex = sex;
    }
    return self;
}

-(NSComparisonResult)compareWithName:(Person *)otherPerson{
    //按照姓名升序排序.
 //   return [self.name compare:otherPerson.name];
    //按照姓名降序排序
    return -[self.name compare:otherPerson.name];
}

-(NSComparisonResult)compareWithAge:(Person *)otherPerson{
    if (self.age < otherPerson.age){
        //返回Ascending时,排序方法不会交换对象的位置.
        return NSOrderedAscending;
    } else if(self.age > otherPerson.age){
        //返回Desending时,排序方法会将两个对象交换位置.
        return NSOrderedDescending;
    } else {
        return NSOrderedSame;
    }
}


-(NSString *)description{
    return [NSString stringWithFormat:@"name:%@ age:%ld sex:%@",_name,_age,_sex];
}

@end
